package com.lunhui.search.provider.config;

import org.apache.http.HttpHost;
import org.apache.http.client.config.RequestConfig;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * ES 配置
 */
@Configuration
public class ESConfig {
	@Value("${es.servers:}") private String esServer;
	@Value("${es.cluster:}") private String esCluster;

	@Bean(name = { "restLowLevelClient" }, destroyMethod = "close")
	public RestClient restClient() {
		HttpHost httpHost = new HttpHost(esServer, 9200);
		RestClientBuilder restClientBuilder = RestClient.builder(httpHost);
		restClientBuilder.setRequestConfigCallback(new RestClientBuilder.RequestConfigCallback() {
			@Override
			public RequestConfig.Builder customizeRequestConfig(RequestConfig.Builder requestConfigBuilder) {
				return requestConfigBuilder.setConnectTimeout(5000).setSocketTimeout(5000).setConnectionRequestTimeout(5000);
			}
		});
		RestClient restLowLevelClient = restClientBuilder.build();
		return restLowLevelClient;
	}
}
